package com.zx.zhilian;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zx.common.BeanUtil;
import com.zx.common.C3P0Utils;

public class ZhiLianDao {
	
	private static Logger LOGGER = LoggerFactory.getLogger(ZhiLianDao.class);
	
	public void insert(ZhiLian zhiLian) {
		Connection connection = C3P0Utils.getConnection();
		QueryRunner query = new QueryRunner();
		String sql = "insert into zhiLian values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		
		try {
			Object[] beanToString = BeanUtil.beanToString(zhiLian, ZhiLian.class);
			query.update(connection, sql, beanToString);
		} catch (Exception e) {
			LOGGER.error("保存异常");
			LOGGER.error(e.getMessage(), e);
		} finally {
			C3P0Utils.close(connection, null, null);
		}
	}
	
	public boolean createTable() throws SQLException {
		String tableName = "ZHILIAN"; 
		String createSql = "CREATE TABLE `zhilian` (" + 
				  "`id` int(11) NOT NULL AUTO_INCREMENT," + 
				  "`职位名称` varchar(255) DEFAULT NULL," + 
				  "`职位链接` varchar(255) DEFAULT NULL," + 
				  "`公司名称` varchar(255) DEFAULT NULL," + 
				  "`公司链接` varchar(255) DEFAULT NULL," + 
				  "`公司福利` varchar(255) DEFAULT NULL," + 
				  "`职位月薪` varchar(255) DEFAULT NULL," + 
				  "`工作地点` varchar(255) DEFAULT NULL," + 
				  "`发布日期` varchar(255) DEFAULT NULL," + 
				  "`工作性质` varchar(255) DEFAULT NULL," + 
				  "`工作经验` varchar(255) DEFAULT NULL," + 
				  "`最低学历` varchar(255) DEFAULT NULL," + 
				  "`招聘人数` varchar(255) DEFAULT NULL," + 
				  "`职位类别` varchar(255) DEFAULT NULL," + 
				  "`职位描述` text," + 
				  "`详细工作地点` varchar(255) DEFAULT NULL," + 
				  "`公司介绍` text," + 
				  "`公司规模` varchar(255) DEFAULT NULL," + 
				  "`公司性质` varchar(255) DEFAULT NULL," + 
				  "`公司行业` varchar(255) DEFAULT NULL," + 
				  "`公司主页` varchar(255) DEFAULT NULL," + 
				  "`公司地址` varchar(255) DEFAULT NULL," + 
				  "`关键词` varchar(255) DEFAULT NULL," + 
				  "PRIMARY KEY (`id`)"+
				  ") ENGINE=MyISAM AUTO_INCREMENT=106 DEFAULT CHARSET=utf8";
		try {
			boolean tableExist = C3P0Utils.isTableExist(tableName);
			if(tableExist) {
				C3P0Utils.dropTable(tableName);
			} 
			C3P0Utils.createTable(createSql);
			return true;
		} catch (SQLException e) {
			throw e;
		}
	}
}
